|
1
|
|
|
import { Inject } from '@nestjs/common'; |
|
2
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
|
3
|
|
|
import { CreateLeaveRequestCommand } from './CreateLeaveRequestCommand'; |
|
4
|
|
|
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository'; |
|
5
|
|
|
import { LeaveRequest } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity'; |
|
6
|
|
|
import { DoesLeaveRequestExistForPeriod } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestExistForPeriod'; |
|
7
|
|
|
import { LeaveRequestAlreadyExistForThisPeriodException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestAlreadyExistForThisPeriodException'; |
|
8
|
|
|
import { DoesLeaveExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesLeaveExistForPeriod'; |
|
9
|
|
|
import { EventsOrLeavesAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsOrLeavesAlreadyExistForThisPeriodException'; |
|
10
|
|
|
import { IMattermostNotifier } from 'src/Application/IMattermostNotifier'; |
|
11
|
|
|
import { ICommandBus } from 'src/Application/ICommandBus'; |
|
12
|
|
|
import { CreateNotificationCommand } from 'src/Application/Notification/Command/CreateNotificationCommand'; |
|
13
|
|
|
import { NotificationType } from 'src/Domain/Notification/Notification.entity'; |
|
14
|
|
|
|
|
15
|
|
|
@CommandHandler(CreateLeaveRequestCommand) |
|
16
|
|
|
export class CreateLeaveRequestCommandHandler { |
|
17
|
|
|
constructor( |
|
18
|
|
|
@Inject('ILeaveRequestRepository') |
|
19
|
|
|
private readonly leaveRequestRepository: ILeaveRequestRepository, |
|
20
|
|
|
@Inject('ICommandBus') |
|
21
|
|
|
private readonly commandBus: ICommandBus, |
|
22
|
|
|
private readonly doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod, |
|
23
|
|
|
private readonly doesLeaveExistForPeriod: DoesLeaveExistForPeriod, |
|
24
|
|
|
) {} |
|
25
|
|
|
|
|
26
|
|
|
public async execute(command: CreateLeaveRequestCommand): Promise<string> { |
|
27
|
|
|
const { |
|
28
|
|
|
user, |
|
29
|
|
|
endDate, |
|
30
|
|
|
endsAllDay, |
|
31
|
|
|
type, |
|
32
|
|
|
startDate, |
|
33
|
|
|
startsAllDay, |
|
34
|
|
|
comment |
|
35
|
|
|
} = command; |
|
36
|
|
|
|
|
37
|
|
|
if ( |
|
38
|
|
|
true === |
|
39
|
|
|
(await this.doesLeaveRequestExistForPeriod.isSatisfiedBy( |
|
40
|
|
|
user, |
|
41
|
|
|
startDate, |
|
42
|
|
|
endDate |
|
43
|
|
|
)) |
|
44
|
|
|
) { |
|
45
|
|
|
throw new LeaveRequestAlreadyExistForThisPeriodException(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ( |
|
49
|
|
|
true === |
|
50
|
|
|
(await this.doesLeaveExistForPeriod.isSatisfiedBy( |
|
51
|
|
|
user, |
|
52
|
|
|
startDate, |
|
53
|
|
|
endDate |
|
54
|
|
|
)) |
|
55
|
|
|
) { |
|
56
|
|
|
throw new EventsOrLeavesAlreadyExistForThisPeriodException(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
const leaveRequest = await this.leaveRequestRepository.save( |
|
60
|
|
|
new LeaveRequest( |
|
61
|
|
|
user, |
|
62
|
|
|
type, |
|
63
|
|
|
startDate, |
|
64
|
|
|
startsAllDay, |
|
65
|
|
|
endDate, |
|
66
|
|
|
endsAllDay, |
|
67
|
|
|
comment |
|
68
|
|
|
) |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
this.commandBus.execute( |
|
72
|
|
|
new CreateNotificationCommand( |
|
73
|
|
|
NotificationType.POST, |
|
74
|
|
|
'Demande de congés', |
|
75
|
|
|
leaveRequest |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
return leaveRequest.getId(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|